﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Perform
{
    public partial class frmInventoryUpdate : Form
    {
        public frmInventoryUpdate()
        {
            InitializeComponent();
        }

        private void frmInventoryUpdate_Load(object sender, EventArgs e)
        {
            string sql = "select iname from inventory";
            SqlDataReader sdr = Dal.DataHelper.ExecuteReader(sql);
            while (sdr.Read()) { cmbfid.Items.Add(sdr["iname"].ToString()); }
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void cmbfid_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql = string.Format("select fid, inumber, ioutput, ileft from inventory where iname = '{0}'", cmbfid.Text.Trim());
            SqlDataReader rd = Dal.DataHelper.ExecuteReader(sql);
            while (rd.Read())
            {
                tbname.Text = rd["fid"].ToString();
                tbsum.Text = rd["inumber"].ToString();
                tbout.Text = rd["ioutput"].ToString();
                tbresidue.Text = rd["ileft"].ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int iout = Convert.ToInt32(tbout.Text.Trim());
            int sum = Convert.ToInt32(tbsum.Text.Trim());
            tbresidue.Text = (sum - iout).ToString();
            try
            {
                string sql = string.Format("update inventory set iname='{0}',fid='{1}',inumber='{2}',ioutput='{3}',ileft='{4}' where iname='{5}'", cmbfid.Text.Trim(), tbname.Text.Trim(), sum, iout, tbresidue.Text.Trim(), cmbfid.Text.Trim());
                if (Dal.DataHelper.sqlExecuteNonQuery(sql) > 0)
                {
                    MessageBox.Show("修改仓库信息成功！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    MessageBox.Show("修改仓库信息失败！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }
        }
    }
}
